package com.example.hellodemo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hellodemo.R;

import java.util.List;
import java.util.zip.Inflater;

/**
 * 基于传统Holder的传统Adapter
 */


public class NormalAdapter extends BaseAdapter {
    private Context context;

    public void setList(List<NewsBean> list) {
        this.list = list;
    }

    private List<NewsBean> list;

    public NormalAdapter(Context context, List<NewsBean> list){
       this.context = context;
       this.list = list;

    }

    // 每次都要穿过来一个数组. 并且数组中的数据 不同没有什么联系 则 用泛型

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = View.inflate(context, R.layout.layout_adapter_item,null);
            viewHolder = new ViewHolder();

            viewHolder.titleText = convertView.findViewById(R.id.item_title);
            viewHolder.descText = convertView.findViewById(R.id.item_message);
            viewHolder.timeText = convertView.findViewById(R.id.item_time);
            viewHolder.phoneText = convertView.findViewById(R.id.item_phone);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"postion"+ position,Toast.LENGTH_LONG).show();
                }
            });
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        NewsBean bean = list.get(position);
        viewHolder.titleText.setText(bean.getTitle());
        viewHolder.descText.setText(bean.getMessage());
        viewHolder.timeText.setText(bean.getTime());
        viewHolder.phoneText.setText(bean.getPhone());

        return convertView;
    }

    private  class  ViewHolder{
        TextView titleText;
        TextView descText;
        TextView timeText;
        TextView phoneText;
    }
}
